/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ui;

import javax.microedition.lcdui.Graphics;

/**
 *
 * @author kien.tran
 */
public abstract class AFormItem extends AItem {

    protected Style titleStyle, style;
    protected String title;
    protected String[] titleRows = null;

    public AFormItem(String id, String title) {
        this.id = id;
        this.title = title;
    }

    public AFormItem(String id, int x, int y, int width, int height, boolean focus, boolean visible, String title) {
        super(id, x, y, width, height, focus, visible);
        this.title = title;
    }

    public abstract void paint(int x, int y, Graphics g);

    protected abstract void calculateHeight();

    protected abstract void setTitleRows(String title);

    public boolean isPaint(int x, int y) {
        if (!visible) {
            return true;
        }
        calculateHeight();
        int itemX = parent.getItemX(), itemY = parent.getItemY(), parentW = parent.getWidth(), parentH = parent.getHeight() - parent.getFooterHeight();
        if (x < itemX || x > parentW) {
            return false;
        }

        if (y + height < itemY || y > parentH) {
            return false;
        }

        return true;
    }

    public boolean isSelectedPaint(int x, int y) {
        if (!visible) {
            return true;
        }
        calculateHeight();
        int itemX = parent.getItemX(), itemY = parent.getItemY(), parentW = parent.getWidth(), parentH = parent.getHeight() - parent.getFooterHeight();
        if (x < itemX || x + width > parentW) {
            return false;
        }

        if (y < itemY || y + height > parentH) {
            return false;
        }

        return true;
    }

    public String getTitle() {
        return title;
    }

    public int getHeight() {
        calculateHeight();
        return this.height;
    }

    public void setParent(ACanvas parent) {
        super.setParent(parent);
        if (parent != null) {
            this.style = parent.getStyle().getItemStyle();
            this.titleStyle = parent.getStyle().getTextStyle();
        }
    }

    public void setTitle(String title) {
        this.title = title;
        setTitleRows(title);
    }
}
